<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-17 13:52
 *
 * Version: 2.0
 */

namespace App\Modules\Employee\Services;


use App\Exceptions\NotFoundException;
use App\Lib\Utilities;
use App\Modules\Employee\Criterias\GetWxBarberInfoCriteria;
use App\Modules\Employee\Criterias\GetWxPortfolioListCriteria;
use App\Modules\Employee\Repositories\PortfolioLikeRepository;
use App\Modules\Employee\Repositories\PortfolioReplyRepository;
use App\Modules\Employee\Repositories\PortfolioRepository;
use App\Modules\Employee\Repositories\PortfolioResourceRepository;
use App\Modules\Employee\Repositories\UserLikeRepository;
use Flower\Core\Repository\Eloquent\BaseService;
use Illuminate\Support\Facades\DB;

class PortfolioService extends BaseService
{
	private $portfolioRepository;
	private $portfolioResourceRepository;
	private $portfolioLikeRepository;
	private $portfolioReplyRepository;
	private $userLikeRepository;

	public function __construct(PortfolioRepository $portfolioRepository, PortfolioResourceRepository $portfolioResourceRepository, PortfolioReplyRepository $portfolioReplyRepository, PortfolioLikeRepository $portfolioLikeRepository,UserLikeRepository $userLikeRepository)
	{
		$this->portfolioRepository = $portfolioRepository;
		$this->portfolioLikeRepository = $portfolioLikeRepository;
		$this->portfolioReplyRepository = $portfolioReplyRepository;
		$this->portfolioResourceRepository = $portfolioResourceRepository;
		$this->userLikeRepository = $userLikeRepository;
	}

	/**
	 * 新增或编辑作品集
	 * @param $barberId
	 * @param $name
	 * @param $serviceId
	 * @param $resourceId
	 * @param $desc
	 * @param $storeId
	 * @param int $portfolioId
	 * @return mixed
	 * @throws NotFoundException
	 */
	public function addPortFolio($barberId,$name,$serviceId,$resourceId,$desc,$storeId,$portfolioId=0)
	{
		DB::beginTransaction();
		if($portfolioId){
			$portfolio = $this->portfolioRepository->isExisted($portfolioId);
			$portfolio = $this->portfolioRepository->editPortfolio($portfolioId,$barberId,$name,$serviceId,$desc,$storeId);
		}else{
			$portfolio = $this->portfolioRepository->addPortfolio($barberId,$name,$serviceId,$desc,$storeId);
		}
		// 获取上传的 视频和图片
		$res = $this->portfolioResourceRepository->addResource($portfolio->id,$resourceId);
		$res->toArray() ? DB::commit() : DB::rollBack();
		return $res;
	}

	/**
	 * 查询作品集
	 * @param $storeId
	 * @param $barberId
	 * @param $pageSize
	 * @param null $serviceName
	 * @param int $memberId
	 * @param null $portfolio_id
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getPortFolioList($storeId,$barberId,$pageSize,$protfolioName=null,$memberId=0,$portfolio_id=null)
	{
		//todo 根据经纬度查询店铺列表
//		$longitude = $requestData['longitude'];
//		$latitude = $requestData['latitude'];
		$pageSize = isset($pageSize) ? $pageSize : 10;
		$list = $this->portfolioRepository->getListByUserIdAndBarberId($storeId,$barberId,$pageSize,$protfolioName,$memberId,$portfolio_id);
		return $list;
	}

	/**
	 * 删除作品集
	 * @param $barberId
	 * @param $portfolioId
	 * @return int
	 * @throws NotFoundException
	 */
	public function delPortfolio($barberId,$portfolioId)
	{
		$portfolio = $this->portfolioRepository->isExisted($portfolioId);
		if($portfolio->user_id != $barberId){
			throw new NotFoundException(['msg'=>'您对该作品集无权操作！']);
		}
		return $this->portfolioRepository->delete($portfolioId);
	}

	/**
	 * 点赞作品
	 * @param $memberId
	 * @param $portfolioId
	 * @return mixed
	 */
	public function likePortfolio($memberId,$portfolioId)
	{
		return $this->portfolioLikeRepository->addLikePortfolio($memberId,$portfolioId);
	}

	/**
	 * 取消点赞
	 * @param $likePortfolioId
	 * @return int
	 */
	public function delLike($memberId,$portfolioId)
	{
		return $this->portfolioLikeRepository->deletePortfolio($memberId,$portfolioId);
	}

	/**
	 * 新增回复
	 * @param $requestData
	 * @return mixed
	 */
	public function addReply($requestData)
	{
		return $this->portfolioReplyRepository->create($requestData);
	}

	/**
	 * 删除回复
	 * @param $requestData
	 * @return int
	 */
	public function delReply($requestData)
	{
		return $this->portfolioReplyRepository->deleteWhere($requestData);
	}

	/**
	 * 作品集播放次数加1
	 * @param $requestData
	 * @return mixed
	 */
	public function setInc($requestData){
		return $this->portfolioRepository->setInc($requestData);
	}


	/**
	 * 收藏的作品集
	 */
	public function getLikePortfolioList($requestData){
		return [];
	}

	/**
	 *  理发师预约头部信息wx
	 * @param $portfolioId
	 * @return mixed
	 */
	public function getAppointmentBg($portfolioId){
		return $this->portfolioRepository->getAppointmentBgById($portfolioId);
	}

	/**
	 * 最新关注的理发师作品集
	 * @param $memberId
	 * @return mixed
	 */
	public function getNewLikePortfolio($memberId,$pageSize=null){
		return $this->userLikeRepository->getNewLikePortfolioByMemberId($memberId,$pageSize);
	}


	/**
	 * 用户是否关注理发师和点赞作品集
	 * @param $memberId
	 * @param $portfolioId
	 * @param $barberId
	 * @return mixed
	 */
	public function isLikeAndFollow($memberId,$portfolioId,$barberId){
		$list['isLike'] = $this->portfolioLikeRepository->isLike($memberId,$portfolioId);
		$list['isFollow'] = $this->userLikeRepository->isFollow($memberId,$barberId);
		$list['playTimes'] = $this->portfolioRepository->getPlayTimes($portfolioId);
		return $list;
	}
}